package com.itheima.manager;

import com.alibaba.fastjson.JSON;
import com.itheima.domain.*;
import com.itheima.interceptor.AdminHolder;
import com.itheima.service.db.*;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import com.itheima.util.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class UmsAdminManager {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @DubboReference
    UmsAdminService umsAdminService;

    @DubboReference
    UmsAdminRoleRelationService umsAdminRoleRelationService;

    @DubboReference
    UmsRoleService umsRoleService;

    @DubboReference
    UmsRoleMenuRelationService umsRoleMenuRelationService;

    @DubboReference
    UmsMenuService umsMenuService;

    //登陆
    public R login(Map<String, String> map) {
        String username = map.get("username");
        String password = map.get("password");
        UmsAdmin umsAdmin = umsAdminService.findByOne(username, password);

        if (umsAdmin == null){
            return R.error(403,"用户名或密码错误!");
        }

        //生成token
        Map<String,Object> adminMap = new HashMap<>();
        adminMap.put("id",umsAdmin.getId());
        adminMap.put("username",umsAdmin.getUsername());
        String token = JwtUtil.createToken(adminMap);

        //token作为key,user的json字符串作为value存入redis中,设置时效30天
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN+token, JSON.toJSONString(umsAdmin), Duration.ofDays(30));

        //准备返回数据
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("token",token);
        resultMap.put("tokenHead","Bearer ");
        return R.success(resultMap);
    }

    //获取当前登录用户信息
    public R getAdminInfo() {
        //获取当前用户adminId
        UmsAdmin umsAdmin = AdminHolder.get();

        //根据id查询umsAdminRoleRelation表,获取roleId
        Long roleId = umsAdminRoleRelationService.findByAdminId(umsAdmin.getId());

        //根据roleId查询umsRole表,获取name
        UmsRole umsRole = umsRoleService.findById(roleId);

        //根据roleId查询UmsRoleMenuRelation表,获取menuId集合
        List<UmsRoleMenuRelation> umsRoleMenuRelationList = umsRoleMenuRelationService.findByRoleId(roleId);

        //遍历集合根据id查询UmsMenu表
        List<UmsMenu> menuList = umsRoleMenuRelationList.stream()
                .map(umsRoleMenuRelation -> umsMenuService.findById(umsRoleMenuRelation.getMenuId()))
                .collect(Collectors.toList());

        //封装数据
        Map<String,Object> map = new HashMap<>();
        //TODO List集合
        map.put("roles",umsRole.getName());
        map.put("icon",umsAdmin.getIcon());
        map.put("menus",menuList);
        map.put("username",umsAdmin.getUsername());

        return R.success(map);
    }

    //登出
    public R logout(String token) {
        //去除前缀
        token = token.replaceAll("Bearer ","");

        //删除redis中缓存
        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN+token);

        return R.success(null);
    }

    //刷新token
    public R refreshToken(String token) {
        //移除前缀
        token = token.replaceAll("Bearer ","");

        //获取value
        String umsAdminJson = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_TOKEN + token);

        //将redis中token续期一个月
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN+token,umsAdminJson, Duration.ofDays(30));

        //准备返回数据
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("token",token);
        resultMap.put("tokenHead","Bearer ");
        return R.success(resultMap);
    }
}
